using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Typing_GUI.CLASS;

namespace Typing_GUI
{
    public partial class frmManageUsers : Form
    {
        public frmManageUsers()
        {
            InitializeComponent();
        }
        #region " ID _ NAME"
       public int m_id;

        public int Id
        {
            get { return m_id; }
            set { m_id = value; }
        }

        public int userID;

        private string m_FullName;

        public string FullName
        {
            get { return m_FullName; }
            set { m_FullName = value; }
        }


  
        #endregion

        #region " Ham"
        public void LoadDanhSachNguoiDung()
        {
//            Id = 5;
            string tenNguoiDung = CNguoiDung.LoadThongTinNguoiDung(userID, pbAvatar, pbCapDo);
            lblHoTen.Text = tenNguoiDung;
            DataTable dt = CNguoiDung.LoadDanhSachNguoiDung();
            int stt = 1;
            foreach (DataRow row in dt.Rows)
            {
                DataGridViewRowCollection r = this.dgvUserList.Rows;
                string bn = "";
                if ((bool)row["Bannick"] == true)
                {
                    bn = "Bannicked";
                }
                else
                    bn = "Not Bannicked";
                r.Add(stt.ToString(), row["TenDangNhap"], row["HoTen"], ((DateTime)row["NgaySinh"]).ToString("dd/MM/yyyy"), row["DiaChi"], row["Email"], row["TenCapDo"], row["TongDiem"], bn);
                stt++;
            }
        }

        public void ReFreshForm()
        {
            dgvUserList.Rows.Clear();
            LoadDanhSachNguoiDung();
            dgvUserList.SelectionMode = DataGridViewSelectionMode.CellSelect;
            dgvUserList.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        }
        #endregion

        private void frmManageUsers_Load(object sender, EventArgs e)
        {
            try
            {
                ReFreshForm();              
            }
            catch
            {
                MessageBox.Show("Error connecting database ", "ERROR !!",MessageBoxButtons.OK,MessageBoxIcon.Error); 
            }
            
        }

        private void btnBackToMenu_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnPrevent_Click(object sender, EventArgs e)
        {
            try
            {

                if (dgvUserList.SelectedRows.Count > 0)
                {
                    for (int i = dgvUserList.SelectedRows.Count - 1; i >= 0; i--)
                    {
                        string tenDN = dgvUserList.SelectedRows[i].Cells[1].Value.ToString();
                        if ((string)dgvUserList.SelectedRows[i].Cells[8].Value == "Bannicked")
                        {
                            DialogResult dr = MessageBox.Show("Not Bannicked User : " + tenDN + " !!!", " Warning !!!", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                            if (dr == DialogResult.OK)
                                CNguoiDung.BannickNguoiDung(tenDN, 0);
                        }
                        else
                        {
                            DialogResult dr = MessageBox.Show("Bannicked User : " + tenDN + " !!!", " Warning !!!", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                            if (dr == DialogResult.OK)
                                CNguoiDung.BannickNguoiDung(tenDN, 1);
                        }

                    }
                    ReFreshForm();
                }
                else
                {
                    MessageBox.Show("Not Choose user !!! ", "Warning !!!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch
            {
                MessageBox.Show("Error connecting database !!", " ERROR !!!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {

                if (dgvUserList.SelectedRows.Count > 0)
                {
                    for (int i = 0; i < dgvUserList.SelectedRows.Count; i++)
                    {
                        string tenDN = dgvUserList.SelectedRows[i].Cells[1].Value.ToString();
                        DialogResult dr = MessageBox.Show("Delete User : " + tenDN + " !!!", " Warning !!!", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                        if (dr == DialogResult.OK)
                            CNguoiDung.XoaNguoiDung(tenDN);
                    }
                    ReFreshForm();
                }
                else
                {
                    MessageBox.Show("Not Choose user !!! ", "Warning !!!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch
            {
                MessageBox.Show("Error connecting database !!", " ERROR !!!", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }
        private void MakeVisibleAgain(object sender, FormClosedEventArgs e)
        {
            this.Visible = true;
        }
        //private void lklblSignOut_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        //{
        //    /// chua xu ly sign out 
        //    Form frm = new frmLogin();
        //    frm.FormClosed += new FormClosedEventHandler(MakeVisibleAgain);
        //    frm.Show();
        //    this.Visible = false;
        //}

     

      

       
    }
}